package utils

import (
	"strconv"
	"time"
)

// formatLatency 格式化时间间隔为合适的单位
func FormatLatency(latency time.Duration) string {
	switch {
	case latency < time.Microsecond:
		return strconv.FormatInt(latency.Nanoseconds(), 10) + "ns"
	case latency < time.Millisecond:
		return strconv.FormatFloat(latency.Seconds()*1e6, 'f', 2, 64) + "µs"
	case latency < time.Second:
		return strconv.FormatFloat(latency.Seconds()*1e3, 'f', 2, 64) + "ms"
	default:
		return strconv.FormatFloat(latency.Seconds(), 'f', 2, 64) + "s"
	}
}
